package com.berwin.services;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.berwin.listener.FileTransferListener;
import com.berwin.listener.SocketEventListener;
import com.berwin.network.Session;
import com.berwin.network.SessionFile;

import java.io.File;

public class SocketService extends Service implements SocketEventListener, FileTransferListener {

    public static final String DOWNLOAD_DIR = Environment.getExternalStorageDirectory().getPath() + File.separator + "Syncer";

    private SessionFile sessionFile;
    private Session session;
    private SocketEventListener listener;
    private FileTransferListener fileTransferListener;

    public class CustomBinder extends Binder {

        public void setListener(SocketEventListener listener, FileTransferListener fileTransferListener) {
            SocketService.this.listener = listener;
            SocketService.this.fileTransferListener = fileTransferListener;
        }

        public SocketService getService() {
            return SocketService.this;
        }
    }

    private CustomBinder binder = new CustomBinder();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        String host = intent.getStringExtra("host");
        int port = intent.getIntExtra("port", -1);
        int port_file = intent.getIntExtra("port_file", -1);
        Log.w("SocketService", host + ":" + port);
        this.session = new Session(host, port, this);
        this.session.start();
        this.sessionFile = new SessionFile(host, port_file, this);
        this.sessionFile.start(DOWNLOAD_DIR);
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.w("SocketService", "SocketService-onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onConnect(Session session) {
        if (this.listener == null)
            return;
        this.listener.onConnect(session);
    }

    @Override
    public void onMessage(Session session, JSONObject data) {
        if (this.listener == null)
            return;
        this.listener.onMessage(session, data);

    }

    @Override
    public void onDisconnect(Session session) {
        if (this.listener == null)
            return;
        this.listener.onDisconnect(session);
        this.stopSelf();
    }

    @Override
    public void onStart(SessionFile task, String path, String fileName) {
        if (fileTransferListener != null) {
            fileTransferListener.onStart(task, path, fileName);
        }
    }

    @Override
    public void onProgress(SessionFile task, int percent) {
        if (fileTransferListener != null) {
            fileTransferListener.onProgress(task, percent);
        }
    }

    @Override
    public void onFinished(SessionFile task, String path, String fileName) {
        if (fileTransferListener != null) {
            fileTransferListener.onFinished(task, path, fileName);
        }
    }

    @Override
    public void onError(SessionFile task, String desc) {
        if (fileTransferListener != null) {
            fileTransferListener.onError(task, desc);
        }
    }

    public void send(String text) {
        this.session.send(text);
    }

    public void sendFile(String path) {
        this.sessionFile.upload(path);
    }

}
